package com.hornsun.data.repository;

import com.hornsun.data.dbo.Client;
import com.hornsun.data.dbo.ClientExample;
import com.hornsun.data.dto.WebClientDTO;
import com.hornsun.data.repository.mapper.ClientDTOMapper;
import com.hornsun.data.repository.mapper.ClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by 编译中... on 2018/6/13.
 */
@Repository
public class ClientRepository {
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private ClientDTOMapper clientDTOMapper;


    /**
     * 检测账号密码
     * @param account
     * @param password
     * @return
     */
    public boolean checkAccountInfo(String account , String password){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andAccountEqualTo(account).andPasswordEqualTo(password);
        return clientMapper.countByExample(clientExample)==1;
    }

    /**
     * 通过账号查找
     * @param account
     * @return
     */
    public Client getByAccount(String account){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andAccountEqualTo(account);
        List<Client> clientList  = clientMapper.selectByExample(clientExample);
        if (clientList.size()>0){
            return clientList.get(0);
        }else {
            return null;
        }
    }

    /**
     * 获取所有人员的数量
     * @return
     */
    public int getTotalCount(){
        return (int)clientMapper.countByExample(new ClientExample());
    }


    public Client getByGuid(String guid){
        return clientMapper.selectByPrimaryKey(guid);
    }


    /**
     * 通过guid删除
     * @param guid
     */
    public void deleteByGuid(String guid){
        if (clientMapper.deleteByPrimaryKey(guid)!=1){
            throw new IllegalArgumentException("delete client failed by guid:"+guid);
        }
    }

    /**
     * 新增用户
     * @param client
     */
    public void addClient(Client client){
        if (clientMapper.insertSelective(client)!=1){
            throw new IllegalArgumentException("insert client failed by client:"+client);
        }
    }

    /**
     *
     * 修改用户信息
     * @param client
     */
    public void updateClient(Client client){
        clientMapper.updateByPrimaryKeySelective(client);
    }

    /**
     * 获取所在部门的人员信息（用于前端显示）
     * @param departmentId
     * @return
     */
    public List<WebClientDTO> getByDepartment(int departmentId){
        return clientDTOMapper.getByDepartment(departmentId);
    }


    /**
     * 根据部门和角色获取人员信息（用于前端显示）
     * @param roleGuid
     * @param departmentId
     * @return
     */
    public List<WebClientDTO> getByRoleAndDepartment(String roleGuid , int departmentId){

        return clientDTOMapper.getByDepartmentAndRole(departmentId,roleGuid);
    }

    /**
     * 获取所有人员信息   （用于前端显示）
     * @return
     */
    public List<WebClientDTO> getTotal(){
        return clientDTOMapper.getTotal();
    }


    public boolean accountExist(String account){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andAccountEqualTo(account);
        return clientMapper.countByExample(clientExample)>0;
    }

    public boolean hasDepartment(int id){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andDepartmentIdEqualTo(id);
        return clientMapper.countByExample(clientExample)>0;
    }

    public void updateByAccount(Client client,String account){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andAccountEqualTo(account);
        clientMapper.updateByExampleSelective(client,clientExample);
    }


    public long countByRole(String roleGuid){
        ClientExample clientExample = new ClientExample();
        ClientExample.Criteria criteria = clientExample.createCriteria();
        criteria.andRoleGuidEqualTo(roleGuid);
        return clientMapper.countByExample(clientExample);
    }



}
